cls
#Variables
$Domain = '<tenant>.mail.onmicrosoft.com'
#Get all users in ActiveDirectory
$Users = Get-ADUser -Filter 'msExchMailboxGuid -like "*"' -Properties ProxyAddresses
#Some output is always nice
Write-Host -Object "Processing $Users.Count users..." -ForegroundColor Green
#Go through all users
foreach ($User in $Users)
{
if (($user.Name -notlike "*mailbox*") -and ($user.Name -notlike "*federated*"))
{
#Check if <domain>.mail.onmicrosoft.com alias is present, if not add it
if ($User.Proxyaddresses -like "*$Domain*")
{
#Write-Host -Object "$User.SamAccountName has an alias matching $Domain..." -ForegroundColor Yellow
}
else
{
$Alias = $User.SamAccountName + '@' + $Domain
Set-ADUser $User -Add @{
Proxyaddresses = "$Alias"
}
Write-Host -Object "Alias added to $User.SamAccountName..." -ForegroundColor Green
}
}
}
Write-Host -Object 'Done' -ForegroundColor Green